package com.bwie.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.DictMapper;
import com.bwie.mapper.HeMapper;
import com.bwie.pojo.Dict;
import com.bwie.pojo.He;
import com.bwie.pojo.Shop;
import com.bwie.service.ShopService;
import com.bwie.mapper.ShopMapper;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author 24026
 * @description 针对表【t_shop】的数据库操作Service实现
 * @createDate 2025-09-09 10:54:03
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop>
        implements ShopService {

    @Autowired
    ShopMapper shopMapper;

    @Autowired
    HeMapper heMapper;
    @Autowired
    StringRedisTemplate redisTemplate;
    @Autowired
    FastFileStorageClient fastFileStorageClient;

    @Override
    public R shopList(PageInfoVo pageInfoVo) {
        Page<Shop> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        Page<Shop> shopPage = shopMapper.shopList(page, pageInfoVo);
        return R.OK(shopPage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R addHeList(Shop shop) {

        He he = new He();
        he.setHeName(shop.getShopName());
        he.setDictType(shop.getDictType());
        he.setHeFu(shop.getShopRen());
        he.setHeStart(new Date());
        he.setHeEnd(new Date());
        he.setDictStatus(shop.getDictStatus());
        he.setHeImages(shop.getHeImages());
        heMapper.insert(he);
        return R.OK();
    }

    @Autowired
    DictMapper dictMapper;

    @Override
    public R dictList(Integer dictId) {
        List<Dict> dictList = dictMapper.selectList(new QueryWrapper<Dict>().lambda().eq(Dict::getDictType, dictId));
        redisTemplate.opsForValue().set("dict", JSON.toJSONString(dictList), 60, TimeUnit.MINUTES);
        return R.OK(dictList);
    }

    @Override
    public R heList(PageInfoVo pageInfoVo) {
        Page<He> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        Page<He> hePage = heMapper.heList(page, pageInfoVo);
        return R.OK(hePage);
    }

    @Override
    public R putList(Shop shop) {
//        Shop shop1 = shopMapper.selectOne(new QueryWrapper<Shop>().lambda().eq(Shop::getShopId, shop.getShopId()));
//        if (shop1 == null) {
//            return R.ERROR(500, "修改失败");
//        }
        shop.setShopStatus("正常");
        shopMapper.updateById(shop);
        return R.OK();
    }

    @Override
    public R sendFile(MultipartFile file) {

        try {
            String exName = FileNameUtil.extName(file.getOriginalFilename());
            String fullPath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), exName, null).getFullPath();
//            String path = fastFileStorageClient.uploadFile(file.getInputStream(),file.getSize(),exName,null).getPath();
            String url = "http://192.168.80.192:8888/" + fullPath;
            return R.OK(url);
        } catch (IOException e) {
//            throw new RuntimeException(e);
            e.printStackTrace();
          return R.ERROR(500,"失败");
        }
    }



    @Override
    public R deleteList(Integer shopId) {
        shopMapper.deleteById(shopId);

        return R.OK();
    }


}




